package cn.ac.jaas.isc.vegetable.pbms.controller;

import cn.ac.jaas.isc.vegetable.pbms.api.vo.GroupIdAndDeviceIdVO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.ac.jaas.isc.vegetable.common.core.util.R;
import cn.ac.jaas.isc.vegetable.common.log.annotation.SysLog;
import cn.ac.jaas.isc.vegetable.pbms.api.entity.ArcGroupModel;
import cn.ac.jaas.isc.vegetable.pbms.service.ArcGroupModelService;
import org.springframework.security.access.prepost.PreAuthorize;
import cn.ac.jaas.isc.vegetable.common.excel.annotation.ResponseExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author 张高昌
 * @date 2022-05-28 17:25:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/arcgroupmodel")
@Api(value = "arcgroupmodel", tags = "管理")
public class ArcGroupModelController {

    private final ArcGroupModelService arcGroupModelService;

    /**
     * 分页查询
     *
     * @param page          分页对象
     * @param arcGroupModel
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('pbms_arcgroupmodel_view')")
    public R getArcGroupModelPage(Page page, ArcGroupModel arcGroupModel) {
        return R.ok(arcGroupModelService.page(page, Wrappers.query(arcGroupModel)));
    }


    /**
     * 通过id查询
     *
     * @param modelId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{modelId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcgroupmodel_view')")
    public R getById(@PathVariable("modelId") Integer modelId) {
        return R.ok(arcGroupModelService.getById(modelId));
    }

    /**
     * 新增
     *
     * @param arcGroupModel
     * @return R
     */
    @ApiOperation(value = "新增", notes = "新增")
    @SysLog("新增")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('pbms_arcgroupmodel_add')")
    public R save(@RequestBody @Valid ArcGroupModel arcGroupModel) {
        return R.ok(arcGroupModelService.save(arcGroupModel));
    }

    /**
     * 修改
     *
     * @param arcGroupModel
     * @return R
     */
    @ApiOperation(value = "修改", notes = "修改")
    @SysLog("修改")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('pbms_arcgroupmodel_edit')")
    public R updateById(@RequestBody @Valid ArcGroupModel arcGroupModel) {
        return R.ok(arcGroupModelService.updateById(arcGroupModel));
    }

    /**
     * 通过id删除
     *
     * @param modelId id
     * @return R
     */
    @ApiOperation(value = "通过id删除", notes = "通过id删除")
    @SysLog("通过id删除")
    @DeleteMapping("/{modelId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcgroupmodel_del')")
    public R removeById(@PathVariable Integer modelId) {
        return R.ok(arcGroupModelService.removeById(modelId));
    }


    /**
     * 导出excel 表格
     *
     * @param arcGroupModel 查询条件
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('pbms_arcgroupmodel_export')")
    public List<ArcGroupModel> export(ArcGroupModel arcGroupModel) {
        return arcGroupModelService.list(Wrappers.query(arcGroupModel));
    }

    /**
     * 分页查询已绑定的装置
     *
     * @param houseId
     * @param groupId
     * @return
     */
    @GetMapping("/getGroupMemberPage")
    @PreAuthorize("@pms.hasPermission('pbms_arcgroupmodel_view')")
    public R getGroupMemberPage(Page page, Integer houseId, Integer groupId) {
        if (houseId == null) {
            return R.failed("温室id不能为空");
        }
        if (groupId == null) {
            return R.failed("分组id不能为空");
        }
        return R.ok(arcGroupModelService.getGroupPage(page, houseId, groupId));
    }

    /**
     * 通过id移除装置
     *
     * @param modelId id
     * @return R
     */
    @ApiOperation(value = "通过id移除装置", notes = "通过id移除装置")
    @SysLog("通过id移除装置")
    @DeleteMapping("/removeDevice/{modelId}")
    @PreAuthorize("@pms.hasPermission('pbms_arcgroupmodel_del')")
    public R removeDevice(@PathVariable Integer modelId) {
        return R.ok(arcGroupModelService.removeDeviceById(modelId));
    }

    /**
     * 绑定分组装置
     *
     * @param groupId
     * @return
     */
    @ApiOperation(value = "绑定分组装置", notes = "绑定分组装置")
    @PutMapping("bindingDevice")
    @PreAuthorize("@pms.hasPermission('pbms_arcgroupmodel_edit')")
    public R bindingDevice(@RequestBody @Valid GroupIdAndDeviceIdVO vo) {
        return R.ok(arcGroupModelService.bindingDevice(vo));
    }

}
